﻿using HtmlAgilityPack;
using System.Text.RegularExpressions;

namespace Cxuu.Common
{
    public sealed class TrustedHtmlContainer
    {
        public TrustedHtml TrustedHtml { get; set; }
    }

    public class HtmlUtility
    {

        /// <summary>
        /// 移除Html标签
        /// </summary>
        /// <param name="rawString">待处理字符串</param>
        /// <param name="removeHtmlEntities">是否移除Html实体</param>
        /// <param name="enableMultiLine">是否保留换行符</param>
        /// <returns></returns>
        public static string StripHtml(string rawString, bool removeHtmlEntities, bool enableMultiLine)
        {
            if (string.IsNullOrWhiteSpace(rawString))
                return string.Empty;
            string input = rawString;
            if (enableMultiLine)
            {
                input = Regex.Replace(Regex.Replace(input, @"</p(?:\s*)>(?:\s*)<p(?:\s*)>", "\n\n", RegexOptions.Compiled | RegexOptions.IgnoreCase), @"<br(?:\s*)/>", "\n", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            }
            input = input.Replace("\"", "''");
            if (removeHtmlEntities)
            {
                input = Regex.Replace(input, "&[^;]*;", string.Empty, RegexOptions.Compiled | RegexOptions.IgnoreCase);
            }
            return Regex.Replace(input, "<[^>]+>", string.Empty, RegexOptions.Compiled | RegexOptions.IgnoreCase);
        }



        /// <summary>
        /// 闭合未闭合的Html标签 
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static string CloseHtmlTags(string html)
        {
            if (string.IsNullOrEmpty(html))
            {
                return html;
            }
            HtmlDocument document = new()
            {
                OptionAutoCloseOnEnd = true,
                OptionWriteEmptyNodes = true
            };
            document.LoadHtml(html);
            return document.DocumentNode.WriteTo();
        }

        /// <summary>
        /// 清除UBB标签 
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string StripBBTags(string content)
        {
            return Regex.Replace(content, @"\[[^\]]*?\]", string.Empty, RegexOptions.IgnoreCase);
        }

        /// <summary>
        /// 移除Html用于内容预览 
        /// </summary>
        /// <param name="rawString"></param>
        /// <returns></returns>
        public static string StripForPreview(string rawString)
        {
            return StripBBTags(StripHtml(rawString.Replace("<br>", "\n").Replace("<br/>", "\n").Replace("<br />", "\n").Replace("<p>", "\n").Replace("'", "&#39;"), false, false));
        }


        /// <summary>
        /// 移除script标签
        /// </summary>
        /// <param name="rawString"></param>
        /// <returns></returns>
        public static string StripScriptTags(string rawString)
        {
            rawString = Regex.Replace(rawString, "<script((.|\n)*?)</script>", "", RegexOptions.Multiline | RegexOptions.IgnoreCase);
            rawString = rawString.Replace("\"javascript:", "\"");
            return rawString;
        }

        /// <summary>
        /// 移除html内的Elemtnts/Attributes及 ，超过charLimit个字符进行截断
        /// </summary>
        /// <param name="rawHtml"></param>
        /// <param name="charLimit"></param>
        /// <returns></returns>
        public static string TrimHtml(string rawHtml, int charLimit)
        {
            if (string.IsNullOrEmpty(rawHtml))
            {
                return string.Empty;
            }
            string rawString = StripBBTags(StripHtml(rawHtml, true, false));
            if ((charLimit > 0) && (charLimit < rawString.Length))
            {
                return StringUtility.Trim(rawString, charLimit);
            }
            return rawString;
        }
    }
}
